// packageG/mycomponent/supplyChain/testall/testall.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  options: {
    multipleSlots: true, // 复数插槽: 是
    addGlobalClass: true,
  },
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
    index: {
      type: null,
    },
    btnFlag: {
      type: null
    },
    U_tabcontrol: {
      type: null,
    },
    isLast: {
      type: null,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    page:1,
    arrIndex: 1, //组件加载下一页
    isLoadMore: true,
    allLoaded: false, //全部数据加载完成
    totalPage:0,
    tiktokList:[],
    tiktokUnList:[]
  },
  lifetimes: {
    created() {
    },
    attached: function () {
    }
  },
  observers: {
    datas: function (data) {
      if (this.data.totalPage <= this.data.page) {
        this.setData({
          isLoadMore: false,
          allLoaded: true,
        });
      }
    },
    bottomShowBol: function (flag) {
      if (this.data.U_tabcontrol) {
        if(flag && this.data.isLast && this.data.isLoadMore) {
          this.getMoreData();
        }
      }
      if (flag && this.data.bottomShowBolIndex == this.data.index && this.data.isLoadMore) {
        this.getMoreData();
      }
    },
  },
  /**
   * 组件的方法列表
   */
  ready() {
    this.setListData('tiktokList');
  },
  methods: {
    link_goods(event) {
      let {product_id} = event.currentTarget.dataset.item;
      wx.navigateTo({
        url: '/packageF/cps/detail/detail?goods_id='+product_id,
      })
    },
    setListData(setList) { //处理列表数据是自动获取还是手动获取
      let dataList = this.data.datas.list.data;
      let tikTokArr = [];
      if (dataList) {
        this.setData({
          totalPage: this.data.datas.list.last_page,
        });
        if (this.data.totalPage > this.data.page) {
          this.setData({
            isLoadMore: true,
          });
        }
        if(this.data.datas.get_number > 0 &&  dataList.length > this.data.datas.get_number) { //大于后台控制的数量就显示后台数量列表
          dataList.splice(this.data.datas.get_number,dataList.length-this.data.datas.get_number);
          this.setData({
            isLoadMore:false
          })
        } 
        this.setData({
          [setList]: dataList,
        });
      } else {
        if (this.data.datas.list.length <= 0) {
          this.setData({
            isLoadMore: false,
          });
        }
        if(this.data.datas.list.length > this.data.per_page) {
          this.data.isLoadMoreSet = true;
        }
        if(Array.isArray(this.data.datas.list)) {
          tikTokArr = this.data.datas.list.slice(0, 15);
        }
        this.setData({
          [setList]: tikTokArr || [],
          tiktokUnList:this.data.datas.list || [],
          isLoadMore:false,
          isLoadMoreSet:this.data.isLoadMoreSet
        });
      }
    },
    getMoreData() {
      this.setData({
        isLoadMore:false
      })
      if (this.data.page >= this.data.totalPage) {
        this.setData({
          isLoadMore:false
        })
        return;
      } else {
        this.data.page = this.data.page + 1;
        let json = {
          page:this.data.page,
          decorate_id:this.data.page_id,
          component_id:this.data.component_id,
          component_key:'U_tiktokCps',
        }
        let urlStr = app.getNetAddresss("home-page.get-decorate-page");
        app._postNetWork({
          url: urlStr,
          data:json,
          success:  (resdata) => {
            let res = resdata.data;
            let list = res.data.data;
            if (res.result == 1) {
              this.data.totalPage <= this.data.page?this.data.isLoadMore = false:this.data.isLoadMore =true;
              this.setData({
                isLoadMore:true,
                tiktokList : this.data.tiktokList.concat(list),
              })
              if(this.data.tiktokList.length > this.data.datas.get_number && this.data.datas.get_number != 0) {
                let delList = this.data.tiktokList.splice(this.data.datas.get_number, this.data.tiktokList.length);//删除多余的数组，改变原数组
                this.setData({
                  tiktokList: this.data.tiktokList,
                  isLoadMore:false
                })
              }
            } else {
              this.data.page = this.data.page - 1;
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          },
          fail: function (res) {
            console.log(res);
          }
        });
      }
    }
  }
})